package Test;

/**
 * 模拟一个死锁的过程
 * @author 23737
 * @time 2021.12.3
 */
public class Deadlock {
    private static Object resource1 = new Object();   // 资源1
    private static Object resource2 = new Object();   // 资源2

    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (resource1) {
                System.out.println(Thread.currentThread() + "get resource1");
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting resource2");
                synchronized (resource2){
                    System.out.println(Thread.currentThread() + "get resource2");
                }
            }
        },"线程1").start();


        new Thread(() -> {
            synchronized (resource1) {
                System.out.println(Thread.currentThread() + "get resource2");
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting resource1");
                synchronized (resource2){
                    System.out.println(Thread.currentThread() + "get resource1");
                }
            }
        },"线程1").start();
    }
}
